<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function status($status)
{
    if ($status == 1) {
        $str = "<span class='label label-success radius'>正常</span>";
    } elseif ($status == 0) {
        $str = "<span class='label label-danger radius'>待审</span>";
    } else {
        $str = "<span class='label label-danger radius'>删除</span>";
    }
    return $str;
}

/*
 * 使用curl请求信息
 * $type = 0 get 1 post
 */
function doCurl($url, $type = 0, $data = [])
{
    $ch = curl_init(); //初始化
    curl_setopt($ch, CURLOPT_URL, $url); //设置url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //代表如果成功返回结果
    curl_setopt($ch, CURLOPT_HEADER, 0); //不需要输出header头
    if ($type == 1) {
        curl_setopt($ch, CURLOPT_POST, 1); //post方式请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置post数据
    }
    //执行并获取内容
    $out_put = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    return $out_put;
}

//商户入驻申请的文案
function bisRegister($status)
{
    if ($status == 1) {
        $str = '入驻申请成功';
    } elseif ($status == 0) {
        $str = "待审核,审核通过后平台方会发送邮件通知，请关注邮件";
    } elseif ($status == 2) {
        $str = "非常抱歉，您提交的材料不符合条件，请重新提交";
    } else {
        $str = "该申请已被删除";
    }
    return $str;
}

//分页
function pagination($obj)
{
    if (!$obj) {
        return '';
    }
    //获取url后的参数
    $params = request()->param();
    return '<div class="cl pd-5 bg-1 bk-gray mt-20 tp5-o2o">' . $obj->appends($params)->render() . '</div>';
}

//获取子类city
function getSeCityName($path)
{
    if (empty($path)) {
        return '';
    }
    if (preg_match('/,/', $path)) {
        $city_path = explode(',', $path);
        $cityId = $city_path[1];
    } else {
        $cityId = $path;
    }
    $city = model('city')->get($cityId);
    return $city->name;
}

//获取子分类
function getSeCategoryName($path)
{
    if (empty($path)) {
        return '';
    }
    if (preg_match('/,/', $path)) {
        $cate_path = explode(',', $path);
        $cateId = $cate_path;
    } else {
        $cateId = $path;
    }
    $cate = model('category')->field('name')->whereIn('id', $cateId)->select();
    return $cate;
}

//获取是否是总店
function getMain($main)
{
    if (empty($main)) {
        return false;
    }
    if ($main == 1) {
        return "<span class='label label-success radius'>总店</span>";
    } else {
        return "<span class='label label-danger radius'>分店</span>";
    }
}

//获取团购商品分类
function getDealCategory($path)
{
    if (preg_match('/,/', $path)) {
        $cateId = explode(',', $path);
    } else {
        $cateId = $path;
    }
    $cate = model('category')->whereIn('id', $cateId)->select();
    $data = '';
    foreach ($cate as $v) {
        $data .= ',' . $v->name;
    }
    return trim($data, ',');
}

//获取城市名称
function getCityName($id)
{
    $res = model('city')->whereIn('id', $id)->select();
    foreach ($res as $v) {
        $data[] = $v['name'];
    }
    $data = implode($data, ',');
    return $data;
}

//获取所有的权限信息
function generateTree($data)
{
    $items = array();
    foreach ($data as $v) {
        $items[$v['ps_id']] = $v;
    }
    $tree = array();
    foreach ($items as $k => $item) {
        if (isset($items[$item['ps_pid']])) {
            $items[$item['ps_pid']]['son'][] = &$items[$k];
        } else {
            $tree[] = &$items[$k];
        }
    }
    return getTreeData($tree);
}

function getTreeData($tree, $level = 0)
{
    static $arr = array();
    foreach ($tree as $t) {
        $tmp = $t;
        unset($tmp['son']);
        //$tmp['level'] = $level;
        $arr[] = $tmp;
        if (isset($t['son'])) {
            getTreeData($t['son'], $level + 1);
        }
    }
    return $arr;
}

/**
 * 获取当前控制器名
 */
function getCurrentControllerName()
{
    return getCurrentAction()['controller'];
}

/**
 * 获取当前方法名
 */
function getCurrentMethodName()
{
    return getCurrentAction()['method'];
}


/**
 * 获取当前控制器与操作方法的通用函数
 */
function getCurrentAction()
{
    $action = \Route::current()->getActionName();
    //dd($action);exit;
    //dd($action);
    list($class, $method) = explode('@', $action);
    //$classes = explode(DIRECTORY_SEPARATOR,$class);
    $class = str_replace('Controller', '', substr(strrchr($class, DIRECTORY_SEPARATOR), 1));

    return ['controller' => $class, 'method' => $method];
}

//获取首页商品所属店铺
function countLocation($location)
{
    $data = [];
    $where = [
        'id' => ['in', $location],
        'status' => 1
    ];
    $res = model('bisLocation')->where($where)->select();
    foreach ($res as $v) {
        $data[] = $v['name'];
    }
    $data = implode(',', $data);
    return $data;
}

//生成订单编号
function setOrderSn()
{
    list($a, $b) = explode(' ', microtime());
    $c = explode('.', $a * 10000);
    return $b . $c[0] . mt_rand(100000, 999999);
}
